package py.edu.ucsa.sisale.exceptions;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class ServiceExceptionMapper implements ExceptionMapper<ServiceException> {

	/*
	 * map exception error codes to http responses
	 */
	public Response toResponse(ServiceException exception) {

		switch (exception.getErrorCode()) {
			case PARAMETER_ERROR:
			case PARSING_ERROR:
			case JSON_DECODING_ERROR:
			{
				return Response.status(Status.BAD_REQUEST).entity(exception.getErrorMessage()).build();
			}
			case NO_DATA_FOUND: {
				return Response.status(Status.NOT_FOUND).entity(exception.getErrorMessage()).build();
			}
			case PERSISTENCE_EXCEPTION: {
				return Response.status(Status.BAD_REQUEST).entity(exception.getErrorMessage()).build();
			}
			case LDAP_AUTHENTICATION_EXCEPTION:
			case UNAUTHORIZED: {
				return Response.status(Status.UNAUTHORIZED).entity(exception.getErrorMessage()).build();
			}
			default: {
				return Response.status(Status.BAD_REQUEST).entity(exception.getErrorMessage()).build();
			}
		}
	}
}
